﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Weapp
{
    public partial class SocketClient : Form
    {
        TcpClient myclient;
        private NetworkStream networkStream;
        private StreamReader streamReader;
        private StreamWriter streamWriter;

        public SocketClient()
        {
            InitializeComponent();
            try
            {
                myclient = new TcpClient("localhost", 1234);
            }
            catch
            {
                Console.WriteLine("Failed to connect to server at {0}:999", "localhost");
                return;
            }
            //get a Network stream from the server
            networkStream = myclient.GetStream();
            streamReader = new StreamReader(networkStream);
            streamWriter = new StreamWriter(networkStream);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string s;
                streamWriter.WriteLine("msg1");
                Console.WriteLine("Sending Message 'msg1'");
                streamWriter.Flush();
                s = streamReader.ReadLine();
                Console.WriteLine("Reading Message");
                Console.WriteLine(s);
                textBox1.Text = s;
            }
            catch (Exception ee)
            {
                Console.WriteLine("Exception reading from Server:" + ee.ToString());
            } 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string s;
                streamWriter.WriteLine("msg2");
                Console.WriteLine("Sending Message 'msg2'");
                streamWriter.Flush();
                s = streamReader.ReadLine();
                Console.WriteLine("Reading Message");
                Console.WriteLine(s);
                textBox1.Text = s;
            }
            catch (Exception ee)
            {
                Console.WriteLine("Exception reading from Server:" + ee.ToString());
            } 
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                string s;
                streamWriter.WriteLine("msg3");
                Console.WriteLine("Sending Message 'msg3'");
                streamWriter.Flush();
                s = streamReader.ReadLine();
                Console.WriteLine("Reading Message");
                Console.WriteLine(s);
                textBox1.Text = s;
            }
            catch (Exception ee)
            {
                Console.WriteLine("Exception reading from Server:" + ee.ToString());
            } 
        }
    }
}
